//
//  YSMessageCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/7.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSMessageCtrl.h"
#import "YSClubDataTool.h"
#import "YSCommentMsgFrame.h"
#import "YSCommentMsgCell.h"


@interface YSMessageCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *messageArray;
@property (strong, nonatomic) NSMutableArray *allFrames;
@property (assign, nonatomic) NSInteger pageIndex;
@end

@implementation YSMessageCtrl

-(NSMutableArray *)messageArray{
    if(!_messageArray){
        _messageArray = [NSMutableArray array];
    }
    return _messageArray;
}

- (BOOL)shouldLogin{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setTitle:@"消息"];
    
    _allFrames = [NSMutableArray array];
    
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setBackgroundColor:PUBLIC_BG];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];

    self.pageIndex = 0;
    
    [self reqeustData];

}

-(void)footerRefresh{
    self.pageIndex++;
    [self reqeustData];
}

-(void)reqeustData{
    if(ISLOGIN){
    [YSClubDataTool getMessageListWithPageIndex:self.pageIndex success:^(YSCommentMsgRes *result) {
        [UIUtil refreshComplete:self.tableView];
        if(result.isSucc){
            [self.messageArray addObjectsFromArray:result.allComment];
            [self messageArrayWithFrames];
        }else{
            ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        [UIUtil refreshComplete:self.tableView];
        ShowTips(kRequestFailure);
    }];
    }
}

-(void)messageArrayWithFrames{
    [_allFrames removeAllObjects];
    for (YSCommentMsg *commentMsg in _messageArray) {
        YSCommentMsgFrame *frame = [YSCommentMsgFrame new];
        frame.commentMsg  = commentMsg;
        [_allFrames addObject:frame];
    }
    [_tableView reloadData];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _allFrames.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSCommentMsgCell *cell = [YSCommentMsgCell cellWithTableView:tableView];
    [cell setCommentMsgFrame:_allFrames[indexPath.row]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [_allFrames[indexPath.row] cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

@end
